home *** CD-ROM | disk | FTP | other *** search
- /***********************************************
- * *
- * "DON'T MESS WITH MARILYN" *
- * *
- * MONTE CARLO SIMULATION *
- * *
- * 02/21/91, M.E. Brandt, Ph.D. *
- * *
- ***********************************************/
-
- #include <stdio.h>
-
-
- #define BIT_MASK 7
- #define NO_DOORS 3.0
- #define RANDFACTOR (32768.0 * (1.0/NO_DOORS))
-
-
- main()
- {
-
- int i, n, car, choice, open, count;
-
- unsigned char u[2], doors, noprize, mask, not_open,
- not_choice;
-
- double prob;
-
-
- srand(1);
-
- do {
-
- fprintf(stderr,
- "\nHow many games of not-switching, and switching\
- would you like?: ");
- scanf("%d", &n);
-
- printf("\n\nNumber of trials in this series is %d\n",
- n);
-
-
- /* LOOP IN WHICH CONTESTANT DOES NOT SWITCH */
-
- for(count=i=0; i<n; i++) {
-
- doors = 1;
-
- /* get a random number between 0 and 2 to hide
- car behind door # */
-
- car = (int)((double)rand()/RANDFACTOR);
-
- doors <<= car; /* status of doors */
-
- noprize = ~doors & BIT_MASK; /* doors with goats */
-
- /* contestant picks a door */
-
- choice = (int)((double)rand()/RANDFACTOR);
-
- /* host reveals a door which is not 'choice'
- and not 'car' (a goat!) */
-
- mask = noprize & ((~(1 << choice)) & BIT_MASK);
-
- /* host always picks first one not satisfying
- condition */
-
- open = 0;
- while(!(mask & 1)) {
- mask >>= 1;
- open++;
- }
-
- if(choice == car) count++;
-
- /* since the contestant did not switch, 'choice'
- does not depend on 'open'! */
-
-
- }
-
- prob = (double)count/(double)n;
-
- printf(
- "\nPercentage of wins for non-switching is %.1f",
- prob * 100.0);
-
-
-
- /* LOOP IN WHICH CONTESTANT SWITCHES CHOICE */
-
- for(count=i=0; i<n; i++) {
-
- doors = 1;
-
- /* get a random number between 0 and 2 to hide car
- behind door # */
-
- car = (int)((double)rand()/RANDFACTOR);
-
- doors <<= car; /* status of doors */
-
- noprize = ~doors & BIT_MASK; /* doors with goats */
-
- /* contestant picks a door */
-
- choice = (int)((double)rand()/RANDFACTOR);
-
- /* host reveals a door which is not 'choice' and not
- 'car' */
-
- mask = noprize & ((~(1 << choice)) & BIT_MASK);
-
- open = 0;
- while(!(mask & 1)) {
- mask >>= 1;
- open++;
- }
-
-
- /* now the contestant switches to door which is not
- 'choice' and not 'open' */
-
- not_choice = ~(1 << choice) & BIT_MASK;
- not_open = ~(1 << open) & BIT_MASK;
-
- choice = not_open & not_choice;
-
-
- /* if switched choice = original status of 'doors'
- it's a "win"! */
-
- if(choice == doors) count++;
-
-
- }
-
-
- prob = (double)count/(double)n;
-
- printf("\nPercentage of wins for switching is %.1f\n",
- prob * 100.0);
- printf("\n-----------------------------------------\
- -----\n");
-
- fprintf(stderr, "\nPlay Again?: ");
- scanf("%s", u);
-
- }
- while(toupper(*u) == 'Y');
-
-
- }
-
-